package com.bjpowernode.utils;

import cn.hutool.json.JSONUtil;
import com.bjpowernode.result.R;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * 响应 , r转json后响应到页面
 */
public class ResponseUtil {

    public static void writeResult(R r , HttpServletResponse response) {
        // 将r转为json 后返回给前端
        String resultJSON = JSONUtil.toJsonStr(r);
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.write(resultJSON);
            // 刷新关闭流
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (writer != null){
                writer.close();
            }
        }
    }
}
